define(['zepto','./_itemShow','_base'],function($,itemShow,wanyiBase){
	if(wanyiBase.getCookie('sessionid') == null){
		window.location.href="login.html";
	}
	pageChange();
	function pageChange(){
		if(window.location.hash==""||window.location.hash == "#step1"){
			$('section').addClass('hide');
			$('.actPage1').removeClass('hide');
			window.location.href="./activation.html#step1";
		}
		if(window.location.hash == "#step2"){
			$('section').addClass('hide');
			$('.actPage2').removeClass('hide');
		}
		if(window.location.hash == "#step3"){
			$('section').addClass('hide');
			$('.actPage3').removeClass('hide');
		}
		if(window.location.hash == "#step4"){
			$('section').addClass('hide');
			$('.actPage4').removeClass('hide');
		}
		if(window.location.hash == "#step5"){
			$('section').addClass('hide');
			$('.actPage5').removeClass('hide');
		}
		if(window.location.hash == "#step6"){
			$('section').addClass('hide');
			$('.actPage6').removeClass('hide');
		}
		if(window.location.hash == "#step7"){
			$('section').addClass('hide');
			$('.actPage7').removeClass('hide');
		}
		if(window.location.hash == "#step8"){
			$('section').addClass('hide');
			$('.actPage8').removeClass('hide');
		}
	}
	//
	window.onhashchange = function(){
		pageChange();
	}
	window.onbeforeunload=function(){
		$.ajax({
		    type: 'GET',
		    url: '/mobile/logout/',
		    dataType: 'json',
		    timeout: 3000,
		    success: function(){
		    	wanyiBase.delCookie("sessionid");
		    },
		    error: function(xhr, type){			    	
		    }
		})
		
	}
	var oldHref="#step1";
	function checkHrefChange(){
		if(oldHref == window.location.hash||activation=={}) return;
		oldHref = window.location.hash;
		window.onhashchange();
	}
	setInterval(checkHrefChange, 10);
	//page1
	var activation={};
	activation.usePerson="";
	$('.personBack input').on('click',function(){
		var self=this;
		if($(self).parent().hasClass("on")) return;
		var temp=$('.personBack').filter(".on");
		if (temp) {
			temp.removeClass('on');
		};
		$(self).parent().addClass('on');
		activation.usePerson=$(self).attr('id').toString().trim();
		$('.next1').removeClass('submitDefault');
	})
	//page1show
	function page1Show(){
		$.ajax({
		    type: 'GET',
		    url: '/mobile/check-self/',
		    dataType: 'json',
		    timeout: 3000,
		    success: function(response){
		    	var message = response.message;
		    	if(message == 'yes'){
		    		$(".self").addClass('hide');
		    		info(".actPage1 .pageHead p","你已经激活过自己的盒子。");
		    	}
		    	else{
		    		info(".actPage1 .pageHead p","请正确选择，确定后无法更改。");
		    	}
		    },
		    error: function(xhr, type){			    	
		    }
		})
	}	
	page1Show();
	//page1Next
	$('.next1').on('click',function(){
		if(activation.usePerson!=""){
    		window.location.href="activation.html#step2";
    		pageChange();			  
		}
	})	
	//page2
	activation.sex="";
	$('.sexBack input').on('click',function(){
		var self=this;
		if($(self).parent().hasClass("on")) return;
		var temp=$('.sexBack').filter(".on");
		if (temp) {
			temp.removeClass('on');
		};
		$(self).parent().addClass('on');
		activation.sex=$(self).attr('id').toString().trim();
		if(valiNameFlag){
			$('.next2').removeClass('submitDefault');
		}
	})
	//name
	var elName='#na';
 	var valiNameFlag=false;
 	document.getElementById(elName.replace('#','')).addEventListener('input',function(){
		if($(elName).val().trim()!=""){
			valiNameFlag=true;
		}
		else{
			valiNameFlag=false;
		}
		validateInfo();
	})

	function info(el,info){
		$(el)[0].innerHTML=info;
	}
	//validate
 	function validateInfo(){
 		if(valiNameFlag&&activation.sex!=""){
 			$('.next2').removeClass('submitDefault');
 		}
 		else{
 			$('.next2').addClass('submitDefault');
 		}
 	}
	//page2Next
	$('.next2').on('click',function(){
		var dateFlag=false;
		var time=new Date();
		var tempTime=$('.actPage2 .validate input').val().split('-');
		if(parseInt(time.getFullYear())>parseInt(tempTime[0])&&parseInt(tempTime[0])>=1900){
			dateFlag=true;
		}
		else if(parseInt(time.getFullYear())==parseInt(tempTime[0])){
			if(parseInt(time.getMonth() + 1)>parseInt(tempTime[1])){
				dateFlag=true;
			}
			else if(parseInt(time.getMonth() + 1)==parseInt(tempTime[1])){
				if(parseInt(time.getDate())>=parseInt(tempTime[2])){
					dateFlag=true;
				}
			}
		}
		if(dateFlag){
			info(".actPage2 .info","");
			if(valiNameFlag&&activation.sex!=""){
				var temp=$('.sexBack').filter(".on");
				activation.nameValue=$(elName).val().trim();
				activation.birthday=$('.actPage2 .validate input').val().replace("/","-");
				$(".actPage6 .pageHead i")[0].innerHTML=activation.nameValue;
				//ajax
				$.ajax({
				    type: 'POST',
				    url: '/mobile/check-sample-info/',
				    data: {name: activation.nameValue, gender: activation.sex, birthdate: activation.birthday},
				    dataType: 'json',
				    timeout: 3000,
				    success: function(response){
				    	var code = parseInt(response.code);
				    	if(code == 0){
				    		window.location.href="activation.html#step3";
				    		pageChange();
				    	}
				    	else{
				    		var comment = '';
				    		for(var i = 0,ii = response.message.length;i<ii;i++){
				    			comment += response.message[i].message;
				    		}
				    		info(".actPage2 .info","<i>"+comment+"</i>");
				    	}
				    },
				    error: function(xhr, type){	
				    	window.location.href="login.html";		    	
				    }
				})
				//
			}
			else{
				info(".actPage2 .info","<i>请填写完整您的信息</i>");
			}
		}
		else{
			info(".actPage2 .info","<i>出生日期不正确</i>");
		}
		
	})
	//page3
	var elActCode='#actCode';
	var valiActCodeFlag=false;
	activation.actCode="";
	var preLength=0;
	document.getElementById(elActCode.replace('#','')).addEventListener('input',function(){
		var self=this;
		var temp=$(elActCode).val().toString().trim();
		activation.actCode=temp.replace(/-{1,}/g,'');
		var reg=/\D/;
		if(activation.actCode.match(reg)){	
			$(self).parent().addClass('error');				
			info(".actPage3 .info","<i>激活码不正确</i>");		
		}
		else{
			$(self).parent().removeClass('error');
			info(".actPage3 .info","");		
		}
		if(activation.actCode.length==0){
			$(elActCode).val('');
		}
		if(activation.actCode!=""){				
			if(activation.actCode.length==14){
				valiActCodeFlag=true;
			}
			else{
				valiActCodeFlag=false;
			}
			validatePage3();
		}
		if(activation.actCode.length>2&&activation.actCode.length<=6){
			$(elActCode).val(activation.actCode.replace(/^(.{2})(.*)$/,'$1-$2'));
		}
		if(activation.actCode.length>6&&activation.actCode.length<=10){
			$(elActCode).val(activation.actCode.replace(/^(.{2})(.{4})(.*)$/,'$1-$2-$3'));
		}
		if(activation.actCode.length>10&&activation.actCode.length<=14){
			$(elActCode).val(activation.actCode.replace(/^(.{2})(.{4})(.{4})(.*)$/,'$1-$2-$3-$4'));
		}
	})
	$(elActCode).on('blur',function(event){
			if(activation.actCode.length!=14){				
				info(".actPage3 .info","<i>激活码不正确</i>");
			}
	});
	//validate
 	function validatePage3(){
 		if(valiActCodeFlag){
 			$('.next3').removeClass('submitDefault');
 		}
 		else{
 			$('.next3').addClass('submitDefault');
 		}
 	}
	//page3Next
	var lineActCode;
	var wrongCount=0;
	$('.next3').on('click',function(){		
		if(activation.actCode.length==14){
			wrongCount = wanyiBase.getCookie('wanyiCheckKey');
			if(parseInt(wrongCount) >= 5){
    			info(".actPage3 .info","<i>激活码错误5次，请24小时后再激活</i>");
    			return;
    		}
			lineActCode=$(elActCode).val().trim();
			activation.actCode=$(elActCode).val().trim().replace(/-{1,}/g,'');
			//ajax
 			$.ajax({
	            type: 'GET',
	            url: '/mobile/check-key/'+activation.actCode,
	            dataType: 'json',
	            timeout: 3000,
	            success: function(response){
	              	var status = parseInt(response.code);
		            if(status == 0){
		            	window.location.href="activation.html#step4";
			    		pageChange();
		            }
		            if(status == -1){
		            	var failInfo=response.message;
		                info(".actPage3 .info","<i>"+failInfo+"</i>");
		                wrongCount = wanyiBase.getCookie('wanyiCheckKey');
		            	if(wrongCount == null){
		            		wanyiBase.setCookie('wanyiCheckKey',1,24);
		            	}
		            	else{
		            		wanyiBase.setCookie('wanyiCheckKey',(parseInt(wrongCount)+1),24);
		            	}
		            }
	            },
	            error: function(xhr, type){
	            	wrongCount = wanyiBase.getCookie('wanyiCheckKey');
	            	if(wrongCount == null){
	            		wanyiBase.setCookie('wanyiCheckKey',1,24);
	            	}else{	            		
	            		wanyiBase.setCookie('wanyiCheckKey',(parseInt(wrongCount)+1),24);
	            	}
	            	info(".actPage3 .info","<i>激活码不正确</i>");
	            }
        	})

		}
		else{
			info(".actPage3 .info","<i>激活码不正确</i>");
		}
	})
	//page4
	//clickItem
    var checkCount=0;
   	var arrCheck=[];
  	$('.itemCheck').on('click',function(){
  		var self=this;
  		if($(self).hasClass('checkOn')){
  			$(self).removeClass('checkOn');
        	--checkCount;
        	var temp=$(self).parent().find('.catalog')[0].innerHTML.trim();
        	for(var i=0;i<arrCheck.length;i++){
        		if(arrCheck[i]==temp){
        			break;
        		}
        	}
        	arrCheck.splice(i,1);
  		}
  		else{
	        if(checkCount<5){
	    		$(self).addClass('checkOn');
	          	++checkCount;
	          	arrCheck.push($(self).parent().find('.catalog')[0].innerHTML.trim());
	        }
  		}
		if(checkCount==5){
		$('.next4').removeClass('submitDefault');
		}
		else{
		$('.next4').addClass('submitDefault');
		}
  	})
  	//page4Next
  	$('.next4').on('click',function(){  		
  		if(checkCount==5){
  			window.location.href="activation.html#step5";
			pageChange();
			//read arrCheck
			showDetail(arrCheck);
			var option2={
		      element:$(".navDefault"),
		      elementLevel:4,
		      container:$(".page5NavItem"),
		      defaultClass:"navDefault",
		      actClass:"navOn",
		    }
		    itemShow(option2);
  		}
  	})
	//page5Next
	activation.newArraySort=[];     //sortCategory
  	$('.next5').on('click',function(){  
  		activation.newArraySort=[];		
  		for(var i=0;i<5;i++){
			var current=$('.actPage5 .navItem').eq(i).find('em')[0].innerHTML;
			activation.newArraySort.push(current);	
  		}
  		showDetail2(activation.newArraySort);
  		window.location.href="activation.html#step6";
		pageChange();
  	})
	//page6Next
  	$('.next6').on('click',function(){  		 		
  		window.location.href="activation.html#step7";
		pageChange();
  	})
	//page7Next
  	$('.next7').on('click',function(){  		 		
  		window.location.href="activation.html#step8";
		pageChange();
		//dosomething
		$('.actPage8 .actName')[0].innerHTML=activation.nameValue;
		$('.actPage8 .aCode')[0].innerHTML=lineActCode;
		//ajax
		$.ajax({
            type: 'POST',
            url: '/mobile/activate/',
            data: {sample_key: activation.actCode, belongs:activation.usePerson, agree: 'Y', name:activation.nameValue, gender:activation.sex, birthdate: activation.birthday,concerns:activation.newArraySort.join(',')},
            dataType: 'json',
            timeout: 3000,
            success: function(response){
              	var status = parseInt(response.code);
	            if(status == 0){
	            	//PAGE8show
	            	$('.seccessOrNo i').addClass('seccessIcon');
	            	$('.actPage8 .actSeccess')[0].innerHTML="激活成功";
	            	$('.actPage8 .showMore p')[0].innerHTML="现在你可以<br>使用这个采样盒进行口腔黏膜细胞的采样了";
	            }	
	            else{
	            	$('.actPage8 .actSeccess')[0].innerHTML="激活失败";
	            	$('.actPage8 .showMore p')[0].innerHTML=response.message[0].message;
	            }	    
            },
            error: function(xhr, type){
 				//PAGE8show
 				$('.actPage8 .actSeccess')[0].innerHTML="激活失败";
 				$('.actPage8 .showMore p')[0].innerHTML="请重新激活";
            }
    	})	
		//
  	})
})